#!/bin/sh

echo '================================================================'
echo '          d8888  .d8888b.  88888888888 888b     d888  .d8888b.  '
echo '         d88888 d88P  Y88b     888     8888b   d8888 d88P  Y88b '
echo '        d88P888 888    888     888     88888b.d88888 Y88b.      '
echo '       d88P 888 888            888     888Y88888P888  "Y888b.   '
echo '      d88P  888 888  88888     888     888 Y888P 888     "Y88b. '
echo '     d88P   888 888    888     888     888  Y8P  888       "888 '
echo '    d8888888888 Y88b  d88P     888     888   "   888 Y88b  d88P '
echo '   d88P     888  "Y8888P88     888     888       888  "Y8888P"  '
echo '================================================================'

# 创建日志文件夹
mkdir -p data/admin/logs
mkdir -p data/admin/logs
mkdir -p data/admin/logs

c=0
v=0
if [ $# -eq 0 ]; then
    arr=("admin" "web" "zuul" "vue")
else
    for p in "$@"; do
        if [ "$p" == "web" ]; then
            arr[c]=$p
            c=$(( $c + 1 ))
        elif [ "$p" == "zuul" ]; then
            arr[c]=$p
            c=$(( $c + 1 ))
        elif [ "$p" == "admin" ]; then
            arr[c]=$p
            c=$(( $c + 1 ))
        elif [ "$p" == "vue" ]; then
            v=1
        else
            echo "未知参数，参数只支持: admin、web、zuul、vue"
        fi
    done
    # agtms-vue 最后启动
    if [ $v -eq 1 ]; then
        arr[c]="vue"
    fi
fi

if [ $v -eq 0 ] || [ $c -gt 0 ]; then
    # Maven 打包
    bash mvnw clean install -DskipTests -Ddockerfile.skip=true
    if [ $? -gt 0 ]; then
        echo "maven 构建失败."
        exit
    fi
fi

for i in "${arr[@]}"; do
    if [ "$i" == "web" ]; then
        # 启动 agtms-web
        if [ -f data/web/web.pid ]; then
            kill -9 `cat data/web/web.pid`
            rm data/web/web.pid
            echo "agtms-web 已停止."
        fi
        echo "agtms-web 启动中..."
        java -jar -Xms1g -Xmx1g -Xmn384m -Xss256k agtms-parent/agtms-web/target/agtms-web.jar > data/web/logs/agtms-web.log 2>&1 &
        echo $! > data/web/web.pid
    elif [ "$i" == "zuul" ]; then
        # 启动 agtms-zuul
        if [ -f data/zuul/zuul.pid ]; then
            kill -9 `cat data/zuul/zuul.pid`
            rm data/zuul/zuul.pid
            echo "agtms-zuul 已停止."
        fi
        echo "agtms-zuul 启动中..."
        java -jar -Xms512m -Xmx512m -Xmn192m -Xss256k agtms-zuul/target/agtms-zuul.jar > data/zuul/logs/agtms-zuul.log 2>&1 &
        echo $! > data/zuul/zuul.pid
    elif [ "$i" == "admin" ]; then
        # 启动 agtms-admin
        if [ -f data/admin/admin.pid ]; then
            kill -9 `cat data/admin/admin.pid`
            rm data/admin/admin.pid
            echo "agtms-admin 已停止."
        fi
        echo "agtms-admin 启动中..."
        java -jar -Xms512m -Xmx512m -Xmn192m -Xss256k agtms-admin/target/agtms-admin.jar > data/admin/logs/agtms-admin.log 2>&1 &
        echo $! > data/admin/admin.pid
    elif [ "$i" == "vue" ]; then
        # 启动 agtms-vue
        cd agtms-vue
        echo "agtms-vue 启动中..."
        npm install
        npm run serve
        cd ../
    fi
    sleep 10
    echo "agtms-$i 已启动."
done